﻿using System.Collections.ObjectModel;
using System.Windows;
using Common.Client.Lib.Extensions;
using Common.Lib;
using CommunityToolkit.Mvvm.ComponentModel;
using Syncfusion.Lib.Data;
using Syncfusion.Lib.Extensions;
using Syncfusion.Themes.FluentDark.WPF;

namespace Syncfusion.Lib.Service
{

    /// <summary>
    /// Syncfusion 主题服务
    /// </summary>
    public class SyncfusionThemeService : ObservableObject, ISyncfusionThemeService
    {
        /// <summary>
        /// 缓存节名称
        /// </summary>
        public static string cacheSectionName = typeof(SyncfusionThemeService).AssemblyFullName();

        public SyncfusionThemeService()
        {
            ThemeItemList =
            [
                new ThemeItem() { DisplayName = "Fluent", LightName = "FluentLight", DarkName = "FluentDark" }
                //new ThemeItem(){  DisplayName="FluentHandyControl", LightName="FluentLightHandyControl",DarkName="FluentDarkHandyControl"},
                //new ThemeItem(){  DisplayName="Material",LightName="MaterialLight",DarkName="MaterialDark"},
                //new ThemeItem(){  DisplayName="MaterialBlue",LightName="MaterialLightBlue",DarkName="MaterialDarkBlue"},
                //new ThemeItem(){  DisplayName="Office2019",LightName="Office2019White",DarkName="Office2019Black"},
            ];
            IsDarkTheme = JsonCacheHelper.GetAppJsonCache(cacheSectionName, nameof(IsDarkTheme), false);
            ThemeName = JsonCacheHelper.GetAppJsonCache(cacheSectionName, nameof(SyncfusionThemeService), "Fluent");
        }

        private bool isDarkTheme;
        private ObservableCollection<ThemeItem> themeItemList = [];

        /// <summary>
        /// 是否黑暗主题
        /// </summary>
        public bool IsDarkTheme
        {
            get => isDarkTheme;
            set { isDarkTheme = value; SetProperty(ref isDarkTheme,value); }
        }

        #region 当前主题名称

        /// <summary>
        /// 当前主题名称
        /// </summary>
        private string? _themeName;

        /// <summary>
        /// 当前主题名称
        /// </summary>
        public string? ThemeName
        {
            get => _themeName;
            set => SetProperty(ref _themeName, value);
        }

        #endregion 当前主题名称 

        /// <summary>
        /// 主题项列表
        /// </summary>
        public ObservableCollection<ThemeItem> ThemeItemList
        {
            get => themeItemList;
            set { themeItemList = value ; SetProperty(ref themeItemList, value); }
        }

        /// <summary>
        /// 应用主题名称
        /// </summary>
        /// <param name="displayName"></param>
        public void SetTheme(string displayName)
        {
            JsonCacheHelper.SetAppJsonCache(cacheSectionName, nameof(ThemeName), displayName);
            ThemeName = displayName;
            SetThemeInternal(GetCurrentName());
        }

        public void SetThemeMode()
        {
            IsDarkTheme = !IsDarkTheme;
            JsonCacheHelper.SetAppJsonCache(cacheSectionName, nameof(ThemeName), IsDarkTheme.ToString());
            SetThemeInternal(GetCurrentName());
        }

        private void SetThemeInternal(string themeName)
        {
            Application.Current.MainWindow.SetTheme(themeName);
        }

        public void SetCurrentTheme(DependencyObject dependency)
        {
            dependency.SetTheme(GetCurrentName());
        }

        public string GetCurrentName()
        {
            var item = ThemeItemList.FirstOrDefault(t => t.DisplayName.Equals(ThemeName));
            return (IsDarkTheme ? item?.DarkName : item?.LightName) ?? "";
        }
    }

    /// <summary>
    /// Syncfusion 主题服务
    /// </summary>
    public interface ISyncfusionThemeService
    {
        ObservableCollection<ThemeItem> ThemeItemList { get; set; }

        string GetCurrentName();

        void SetTheme(string themeName);

        void SetThemeMode();

        void SetCurrentTheme(DependencyObject dependency);
    }
}
